package common

import (
	"github.com/gin-gonic/gin"
	"seven-admin/app/admin/schemas/query"
	"seven-admin/app/admin/service/common"
	"seven-admin/app/middleware"
	"seven-admin/core"
	"seven-admin/core/response"
	"strings"
)

var IndexGroup = core.Group("/common", newIndexHandler, regIndex, middleware.TokenAuth())

func newIndexHandler(srv common.IndexService) *indexHandler {
	return &indexHandler{srv: srv}
}

func regIndex(rg *gin.RouterGroup, group *core.GroupBase) error {
	return group.Reg(func(handle *indexHandler) {
		rg.GET("/index/console", handle.console)
		rg.GET("/index/config", handle.config)
		rg.GET("/index/config/tenant", handle.tenantConfig)
	})
}

type indexHandler struct {
	srv common.IndexService
}

// console 控制台
func (ih indexHandler) console(c *gin.Context) {
	res, err := ih.srv.Console(query.GetAuthReq(c))
	response.CheckAndRespWithData(c, res, err)
}

// config 公共配置
func (ih indexHandler) config(c *gin.Context) {
	host := c.Request.Host
	domain := host[:strings.Index(host, ":")]
	res, err := ih.srv.Config(domain)
	response.CheckAndRespWithData(c, res, err)
}

// tenantConfig 租户配置
func (ih indexHandler) tenantConfig(c *gin.Context) {
	res, err := ih.srv.TenantConfig(query.GetAuthReq(c))
	response.CheckAndRespWithData(c, res, err)
}